package ruteodosciudades.algoritmo.extra;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author gonzaleza
 */
public class ConnectDB {
    private String connectionURL;
	private Connection connection = null;
	private String userName, passwrd, dbName;

	public ConnectDB(String database){
		connectionURL  = "jdbc:mysql://localhost:3306/";
  		dbName = database;    //nombre de la base de datos
		userName = "root";  //nombre del user de la bd
		passwrd = "root";      //contrasena en texto plano
	}


	/**
	*	Establece una conexion JDBC con la base de datos postgres
	*/
	//URL jdbc:mysql://{hostIP | hostName}:port[/database]
	public Connection getConnection() throws SQLException{
		// Leer drive de la database
		try{
			Class.forName("com.mysql.jdbc.Driver");
			// Get a Connection to the database
			connection = DriverManager.getConnection(connectionURL + dbName, userName, passwrd);
		}catch(SQLException e){
			System.out.println("SQL ERROR.");
			e.printStackTrace();
		}catch(ClassNotFoundException e){
			System.out.println("CLASS NOT FOUND ERROR.");
			e.printStackTrace();
		}
		return connection;
	}

	/**
	 * Cierra la conexion establecida con la base de datos
	 * @return void
	 * */
	public void cerrarConnection(){
		try
		{
			connection.close();
		}catch(SQLException e){
			System.out.println("SQL ERROR");
			e.printStackTrace();
		}
	}

	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	public String getPasswrd() {
		return passwrd;
	}


	public void setPasswrd(String passwrd) {
		this.passwrd = passwrd;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
}
